#include <stdlib.h>
#include <stdio.h>
#include "writer.h"

Writer::Writer() {

	absoluteErrors = new double[1000];
	relativeErrors = new double[1000];

	cursor = 0;
}

Writer::Writer(int nrIterations) {

	absoluteErrors = new double[nrIterations];
	relativeErrors = new double[nrIterations];

	cursor = 0;
}

void Writer::flushBuffer() {

	absoluteErrors[cursor] = absoluteErrorBuffer/100;
	relativeErrors[cursor] = relativeErrorBuffer;

	cursor++;
}

void Writer::startBuffer() {

	absoluteErrorBuffer = 0;
	relativeErrorBuffer = 0;

}

void Writer::printResultData() {

	double aux = 0;
	FILE *file = fopen("chart/data.js","w");

	fprintf(file,"var absoluteErrors = [\n\t");
	for(int i=0;i<cursor;i++) {
		if (i%50 == 0 && i != 0) {
			fprintf(file,"%0.4lf, ",aux/50);
			aux = 0;
		} else {
			aux += absoluteErrors[i];
		}
	}
	fprintf(file,"\n];\n\n");

	aux = 0;
	fprintf(file,"var relativeErrors = [\n\t");
	for(int i=0;i<cursor;i++) {
		if (i%50 == 0 && i != 0) {
			fprintf(file,"%0.4lf, ",aux/50);
			aux = 0;
		} else {
			aux += relativeErrors[i];
		}
	}
	fprintf(file,"\n];\n\n");

	fprintf(file,"var good = %d\n",goodResults);
	fprintf(file,"var bad = %d\n",badResults);
	
	fclose(file);

}
